package com.infodms.dms.dao.claim.other;

import java.sql.ResultSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.infodms.dms.common.Constant;
import com.infodms.dms.common.Utility;
import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.po.TmDealerPO;
import com.infodms.dms.po.TtAsWrFinePO;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;

@SuppressWarnings("unchecked")
public class BonusDAO extends BaseDao{
	
	
	
	public static Logger logger = Logger.getLogger(BonusDAO.class);
    private static final BonusDAO dao = null;
	
	public static final BonusDAO getInstance() {
	   if(dao==null) return new BonusDAO();
	   return dao;
	}
	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		// TODO Auto-generated method stub
		return null;
	}
	
	/**
	 * 查询经销商
	 * 注：加入经销商级别限制，只查询一级经销商
	 * @param map
	 * @param pageSize
	 * @param curPage
	 * @return
	 */
	public PageResult<TmDealerPO> queryDealer(Map<String,String> map,int pageSize,int curPage) {
		StringBuffer sqlStr=new StringBuffer();
		List params = new LinkedList();
		sqlStr.append("select * from tm_dealer where 1=1 ");
		sqlStr.append(" AND OEM_COMPANY_ID =");
		sqlStr.append(map.get("oemCompanyId"));
//		sqlStr.append(" AND DEALER_LEVEL = " +Constant.DEALER_LEVEL_01+ "\n");
		sqlStr.append(" AND STATUS ="+Constant.STATUS_ENABLE+"\n");
		sqlStr.append(" AND DEALER_TYPE =");
		sqlStr.append(Constant.DEALER_TYPE_DWR);
		sqlStr.append("\n");
		if (Utility.testString(map.get("dealerCode"))){
			sqlStr.append(Utility.getConSqlByParamForEqual(map.get("dealerCode"), params, "tm_dealer", "dealer_code"));
		}
		if (Utility.testString(map.get("dealerName"))){
			sqlStr.append(" and  dealer_name like '%"+map.get("dealerName")+"%' ");
		}
		sqlStr.append(" ORDER BY DEALER_ID DESC ");
		PageResult<TmDealerPO> ps = pageQuery(TmDealerPO.class,sqlStr.toString(),params,pageSize,curPage);
		return ps;
	}
	/**
	 * 
	* @Title: punishQuery 
	* @Description: TODO(处罚历史查询) 
	* @param @param dealerId
	* @param @param pageSize
	* @param @param curPage
	* @param @return    设定文件 
	* @return PageResult<TtAsWrFinePO>    返回类型 
	* @throws
	 */
	public PageResult<Map<String,Object>> punishQuery(String dealerId,
			String deductStartDate,String deductEndDate,String status,String yieldly,
			int pageSize,int curPage) {
		StringBuffer sql = new StringBuffer();
		sql.append("select a.fine_id,\n");
		sql.append("       a.dealer_id,\n");
		sql.append("       (select b.dealer_code from tm_dealer b where b.dealer_id=a.dealer_id) as dealer_code,\n");
		sql.append("       (select b.dealer_code from tm_dealer b where b.dealer_id=a.dealer_id) as dealer_name,\n");
		sql.append("       a.fine_sum,\n");
		sql.append("       a.fine_date,\n");
		sql.append("       a.fine_reason,\n");
		sql.append("       a.remark,\n");
		sql.append("       a.create_by,\n");
		sql.append("       TO_CHAR(a.CREATE_DATE, 'YYYY-MM-dd') AS CREATE_DATE,\n");
		sql.append("       a.pay_status,\n");
		sql.append("       a.pay_date,\n");
		sql.append("       a.claimbalance_id,\n");
		sql.append("       a.yieldly\n");
		sql.append("  from tt_as_wr_fine a\n");
		sql.append(" where a.dealer_id = "+dealerId+"\n");
		
		if (Utility.testString(deductStartDate)) {//提报开始日期
			sql.append("   AND a.pay_date >= TO_DATE('").append(deductStartDate.trim().concat(" 00:00:00")).append("','YYYY-MM-DD HH24:MI:SS') \n");
		}
		if (Utility.testString(deductEndDate)) {//提报结束日期
			sql.append("   AND a.pay_date <= TO_DATE('").append(deductEndDate.trim().concat(" 23:59:59")).append("','YYYY-MM-DD HH24:MI:SS') \n");
		}
		if (Utility.testString(status)) {//提报结束日期
			sql.append("  AND a.pay_status="+status+" \n");
		}
		if (Utility.testString(yieldly)) {//提报结束日期
			sql.append("  AND a.yieldly="+yieldly+" \n");
		}
		sql.append(" order by a.create_date desc");
		PageResult<Map<String,Object>> ps = this.pageQuery(sql.toString(), null, getFunName(), pageSize, curPage);
		return ps;
	}
	/**
	 * 
	* @Title: punishQuery 
	* @Description: TODO(查询一条首页新闻) 
	* @param @param dealerId
	* @param @param pageSize
	* @param @param curPage
	* @param @return    设定文件 
	* @return PageResult<TtAsWrFinePO>    返回类型 
	* @throws
	 */
	public Map<String,Object> queryNewsById(String newsId) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT A.NEWS_CODE,A.NEWS_TITLE,A.NEWS_ID FROM tt_as_wr_news A WHERE A.NEWS_ID="+newsId+"\n");

		Map<String,Object> map = pageQueryMap(sql.toString(), null, getFunName());
		return map;
	}
	
	/**
	 * 经销商查询正负激励
	 * 注：加入经销商级别限制，只查询一级经销商
	 * @param map
	 * @param pageSize
	 * @param curPage
	 * @return
	 */
	public PageResult<TmDealerPO> dlrQueryEncourage(Map<String,String> map,int pageSize,int curPage) {
		StringBuffer sqlStr=new StringBuffer();
		List params = new LinkedList();
		sqlStr.append("select * from tm_dealer where 1=1 ");
		sqlStr.append(" AND OEM_COMPANY_ID =");
		sqlStr.append(map.get("oemCompanyId"));
		sqlStr.append(" AND DEALER_LEVEL = " +Constant.DEALER_LEVEL_01+ "\n");
		sqlStr.append(" AND DEALER_TYPE =");
		sqlStr.append(Constant.DEALER_TYPE_DWR);
		sqlStr.append(" AND DEALER_ID IN ("+map.get("dealerId")+")");
		sqlStr.append("\n");
		if (Utility.testString(map.get("dealerCode"))){
			sqlStr.append(Utility.getConSqlByParamForEqual(map.get("dealerCode"), params, "tm_dealer", "dealer_code"));
		}
		if (Utility.testString(map.get("dealerName"))){
			sqlStr.append(" and  dealer_name like '%"+map.get("dealerName")+"%' ");
		}
		sqlStr.append(" ORDER BY DEALER_ID DESC ");
		PageResult<TmDealerPO> ps = pageQuery(TmDealerPO.class,sqlStr.toString(),params,pageSize,curPage);
		return ps;
	}
	/**
	 * 经销商查询正负激励明细
	 * 注：加入经销商级别限制，只查询一级经销商
	 * @param map
	 * @param pageSize
	 * @param curPage
	 * @return
	 */
	public Map<String,Object> dlrQueryEncourageDetail(String fineId) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT A.FINE_ID,\n");
		sql.append("       A.DEALER_ID,\n");
		sql.append("       (SELECT B.DEALER_CODE FROM TM_DEALER B WHERE A.DEALER_ID=B.DEALER_ID) AS DEALER_CODE,\n");
		sql.append("       (SELECT B.DEALER_NAME FROM TM_DEALER B WHERE A.DEALER_ID=B.DEALER_ID) AS DEALER_NAME,\n");
		sql.append("       A.FINE_SUM,\n");
		sql.append("       A.FINE_DATE,\n");
		sql.append("       A.FINE_REASON,\n");
		sql.append("       A.REMARK,\n");
		sql.append("       A.PAY_STATUS,\n");
		sql.append("       (select y.name from tc_user y where y.user_id=A.CREATE_BY) AS CREATE_BY,\n");
		sql.append("       A.PAY_DATE,\n");
		sql.append("       A.YIELDLY\n");
		sql.append("  FROM TT_AS_WR_FINE A\n");
		sql.append(" WHERE A.FINE_ID = "+fineId+"\n");

		Map<String,Object> map = pageQueryMap(sql.toString(),null,getFunName());
		return map;
	}
	/**
	 * 经销商查询正负激励新闻明细
	 * 注：加入经销商级别限制，只查询一级经销商
	 * @param map
	 * @param pageSize
	 * @param curPage
	 * @return
	 */
	public List<Map<String,Object>> dlrQueryEncourageNewsDetail(String fineId) {
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT B.NEWS_ID,B.NEWS_CODE,B.NEWS_TITLE\n");
		sql.append("  FROM TM_FINE_NEWS A, tt_as_wr_news B\n");
		sql.append(" WHERE A.NEWS_ID = B.NEWS_ID\n");
		sql.append("   AND A.FINE_ID = "+fineId+"\n");


		List<Map<String,Object>> listNews = pageQuery(sql.toString(),null,getFunName());
		return listNews;
	}
}
